/**
 * @description init vuex router
 * @author 盘古他爹
 */

import store from "@/store"
import { newUseStore } from "@/store"
import router from "@/router"
import CacheLocal from "@/utils/cacheLocal"
import { IKey } from "@/store/user/types"
// const store = newUseStore()
export function init() {
  const userInfo = CacheLocal.getItem("LSUserInfo")
  // const router = useRouter()
  if (!userInfo) {
    console.log("没有，返回登陆", router)
    router.push("/login")
    return
  }

  console.log("storedddd", store)

  //vuex
  store.dispatch("moduleUser/autoSaveUserInfo")
  // store.lsActions.moduleUser.autoSaveUserInfo()
  //router
  const menus: IKey[] = CacheLocal.getItem("LSUserInfo").menus
  let routeChildren: any = {}
  console.log("路由", menus)

  menus.forEach((item: IKey) => {
    item.data.forEach((data) => {
      routeChildren.path = data.url
      routeChildren.name = data.url
      routeChildren.component = () =>
        import(`@/views/${item.url}${data.url}.vue`)

      const r = {
        path: data.url,
        name: data.url,
        component: () => import(`@/views/${data.url}.vue`)
      }
      // console.log("routeChildren", routeChildren)

      router.addRoute("main", routeChildren)
      routeChildren = {}
    })
  })

  console.log(router.getRoutes())
}
